package com.cytanaa.youlike;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.cytangaa.display.CustomView;

public class Preferences  extends CustomView {
	Spinner order_spinner;

	SharedPreferences mPrefs;
	String OrderPreference = "relevance";

    @Override
    public void onCreate(Bundle savedInstanceState) {    
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.preference);
		titlebar_title.setText("Prefeerences");
		titlebar_icon.setImageResource(R.drawable.titlebar_preference);
		mPrefs = getSharedPreferences("facebook_session",MODE_PRIVATE);
		String tempPrefs = mPrefs.getString("order", "relevance");
		order_spinner = (Spinner) findViewById(R.id.order_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.order_choice, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    order_spinner.setAdapter(adapter);
	    int defaultInt=0;
	    if(tempPrefs.equals("relevance")){
	    	
	    }else if(tempPrefs.equals("published")){
	    	defaultInt =1;
	    }else{
	    	defaultInt =2;
	    }
	    
	    order_spinner.setSelection(defaultInt);

	    
	    order_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
	    	@Override
	        public void onItemSelected(AdapterView<?> parent,
	            View view, int pos, long id) {
	    		//Get item from spinner and store in string conductorSize........
	    		String temp = parent.getItemAtPosition(pos).toString();
	            if (temp.equals("Relavance"))   { OrderPreference = "relevance"   ;  }
	            if (temp.equals("Date"))   { OrderPreference = "published"   ;  }
	            if (temp.equals("View Count"))   { OrderPreference = "viewCount"   ;  }
	            if (temp.equals("Rate"))   { OrderPreference = "rate" ;  }
	            
	            mPrefs = getSharedPreferences("facebook_session",MODE_PRIVATE);
				SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("order", OrderPreference);
                editor.commit();

	            }
	    	@Override
	        public void onNothingSelected(AdapterView parent) {
	          // Do nothing.
	    		}
	    	} );
	    
    }
    
    public void SetCountry (View v){
    	Intent i = new Intent(this, CountryList.class);	 
        startActivityForResult(i, 1);
        
        Toast.makeText(this, "SetCountry", Toast.LENGTH_LONG).show();
        
    }
    public void SetPrefer (View v){

    	Intent i = new Intent(this, CategoryList.class);	 
        startActivityForResult(i, 1);
        Toast.makeText(this, "SetPrefer", Toast.LENGTH_LONG).show();

    }


}
